package com.syzsn.sk8.controller;

import com.syzsn.common.enums.RecordType;
import com.syzsn.common.entity.SyCountry;
import com.syzsn.sk8.model.SySK8WorldCupGroupRecordModel;
import com.syzsn.sk8.service.SySK8WorldCupService;
import com.syzsn.sk8.entity.*;
import com.syzsn.sk8.enums.SK8Type;
import com.syzsn.sk8.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/worldcup/sk8")
public class SySK8WorldCupController {

    @Autowired
    private SySK8WorldCupRepository sySK8WorldCupRepository;
    @Autowired
    private SySK8WorldCupDivisionRepository sySK8WorldCupDivisionRepository;
    @Autowired
    private SySK8WorldCupGroupRepository sySK8WorldCupGroupRepository;
    @Autowired
    private SySK8WorldCupGroupRecordRepository sySK8WorldCupGroupRecordRepository;
    @Autowired
    private SySK8WorldCupRecordRepository sySK8WorldCupRecordRepository;

    @Autowired
    private SySK8WorldCupService sySK8WorldCupService;

    @GetMapping("/list")
    public ModelAndView list(ModelMap model){
        List<SySK8WorldCup> sySK8WorldCupList = sySK8WorldCupRepository.findAll(Sort.by(Sort.Order.desc("id")));
        model.put("sySK8WorldCupList", sySK8WorldCupList);
        return new ModelAndView("sk8_worldcup_list");
    }

    @GetMapping("/{wcYear}/{wcNum}")
    public ModelAndView info(ModelMap model
            , @PathVariable("wcYear") Integer wcYear
            , @PathVariable("wcNum") Integer wcNum
    ){
        // 获取世界杯信息
        SySK8WorldCup sySK8WorldCup = sySK8WorldCupRepository.findOneByYearAndNum(wcYear, wcNum);
        // 获取世界杯参赛队
        Map<String, List<String>> sk8WorldCupCountryMap = sySK8WorldCupService.getCountryListByWcId(sySK8WorldCup.getId());
        // 获取世界杯分档
        List<SySK8WorldCupDivision> sk8WorldCupDivisionList1 = sySK8WorldCupDivisionRepository.findListByWcIdAndDivision(sySK8WorldCup.getId(), 1);
        List<SySK8WorldCupDivision> sk8WorldCupDivisionList2 = sySK8WorldCupDivisionRepository.findListByWcIdAndDivision(sySK8WorldCup.getId(), 2);
        List<SySK8WorldCupDivision> sk8WorldCupDivisionList3 = sySK8WorldCupDivisionRepository.findListByWcIdAndDivision(sySK8WorldCup.getId(), 3);
        List<SySK8WorldCupDivision> sk8WorldCupDivisionList4 = sySK8WorldCupDivisionRepository.findListByWcIdAndDivision(sySK8WorldCup.getId(), 4);

        // 获取世界杯小组赛
        List<SySK8WorldCupGroup> sySK8WorldCupGroupList = sySK8WorldCupGroupRepository.findListByWcId(sySK8WorldCup.getId());
        // 获取世界杯小组赛比赛
        List<SySK8WorldCupGroupRecord> sySK8WorldCupGroupRecordList = sySK8WorldCupGroupRecordRepository.findListByWcId(sySK8WorldCup.getId());
        List<SySK8WorldCupGroupRecordModel> groupRecordModelList = sySK8WorldCupService.getGroupRecordModelList(sySK8WorldCupGroupRecordList);

        // 获取世界杯淘汰赛-八分之一
        List<SySK8WorldCupRecord> eighthRecordList = sySK8WorldCupRecordRepository.findListByWcIdAndRecordType(sySK8WorldCup.getId(), RecordType.E_EIGHTH.name());
        // 获取世界杯淘汰赛-四分之一
        List<SySK8WorldCupRecord> quarterRecordList = sySK8WorldCupRecordRepository.findListByWcIdAndRecordType(sySK8WorldCup.getId(), RecordType.D_QUARTER.name());
        // 获取世界杯淘汰赛-半决赛
        List<SySK8WorldCupRecord> semiRecordList = sySK8WorldCupRecordRepository.findListByWcIdAndRecordType(sySK8WorldCup.getId(), RecordType.C_SEMI.name());
        // 获取世界杯淘汰赛-三四
        List<SySK8WorldCupRecord> threeFourRecordList = sySK8WorldCupRecordRepository.findListByWcIdAndRecordType(sySK8WorldCup.getId(), RecordType.B_THREE_FOUR.name());
        // 获取世界杯淘汰赛-冠亚
        List<SySK8WorldCupRecord> finalRecordList = sySK8WorldCupRecordRepository.findListByWcIdAndRecordType(sySK8WorldCup.getId(), RecordType.A_FINAL.name());

        model.put("sySK8WorldCup", sySK8WorldCup);
        model.put("sk8WorldCupDivisionList1", sk8WorldCupDivisionList1);
        model.put("sk8WorldCupDivisionList2", sk8WorldCupDivisionList2);
        model.put("sk8WorldCupDivisionList3", sk8WorldCupDivisionList3);
        model.put("sk8WorldCupDivisionList4", sk8WorldCupDivisionList4);
        model.put("sySK8WorldCupGroupList", sySK8WorldCupGroupList);
        model.put("groupRecordModelList", groupRecordModelList);
        model.put("eighthRecordList", eighthRecordList);
        model.put("quarterRecordList", quarterRecordList);
        model.put("semiRecordList", semiRecordList);
        model.put("threeFourRecordList", threeFourRecordList);
        model.put("finalRecordList", finalRecordList);

        model.put("sk8WorldCupCountryMap", sk8WorldCupCountryMap);
//        outputMap(sk8WorldCupCountryMap);
        return new ModelAndView("sk8_worldcup_info");
    }

    private void outputMap(Map<SK8Type, List<SyCountry>> sk8WorldCupCountryMap) {
        for(Map.Entry<SK8Type, List<SyCountry>> entry : sk8WorldCupCountryMap.entrySet()){
            System.out.print(entry.getKey().name()+"--:--");
            for(SyCountry syCountry: entry.getValue()){
                System.out.print("["+syCountry.getCnName()+"]");
                System.out.print(",");
            }
            System.out.println();
        }
    }
}
